Download
FAQ
History
HomeHomeNext API
Search
Feedback
Divider

An Application Example that Deploys a Message-Driven Bean on Two J2EE Servers

This section, like the previous one, explains how to write, compile, package, deploy, and run a pair of J2EE applications that use the JMS API and run on two J2EE servers. The applications are slightly more complex than the ones in the first example.

The applications use the following components:

In this section, the term local server means the server on which both the application client and the message-driven bean are deployed. The term remote server means the server on which only the message-driven bean is deployed.

The section covers the following topics:

You will find the source files for this section in <INSTALL>/j2eetutorial14/examples/jms/sendremote/. Path names in this section are relative to this directory.

Overview of the Applications

This pair of applications is somewhat similar to the applications in An Application Example that Consumes Messages from a Remote J2EE Server in that the only components are a client and a message-driven bean. However, the applications here use these components in more complex ways. One application consists of the application client. The other application contains only the message-driven bean and is deployed twice, once on each server.

The basic steps of the applications are as follows.

  1. The administrator starts two J2EE servers, one on each system.
  2. On the local server, the administrator creates a connection factory to communicate with the remote server.
  3. The application client uses two connection factories--an ordinary one and the one just created--to create two connections, sessions, publishers, and subscribers. Each publisher publishes five messages.
  4. The local and the remote message-driven beans each receive five messages and send replies.
  5. The client's message listener consumes the replies.

Figure 21-5 illustrates the structure of this application.

A J2EE Application that Sends Messages to Two Servers

Figure 21-5 A J2EE Application that Sends Messages to Two Servers

Writing the Application Components

Writing the components of the applications involves

Coding the Application Client: MultiAppServerClient.java

The application client class, multiclient/src/MultiAppServerClient.java, does the following.

  1. It uses the JNDI naming context java:comp/env to look up two connection factories and a topic.
  2. For each connection factory, it creates a connection, a publisher session, a publisher, a subscriber session, a subscriber, and a temporary topic for replies.
  3. Each subscriber sets its message listener, ReplyListener, and starts the connection.
  4. Each publisher publishes five messages and creates a list of the messages the listener should expect.
  5. When each reply arrives, the message listener displays its contents and removes it from the list of expected messages.
  6. When all the messages have arrived, the client exits.

Coding the Message-Driven Bean: ReplyMsgBean.java

The onMessage method of the message-driven bean class, replybean/src/ReplyMsgBean.java, does the following:

  1. Casts the incoming message to a TextMessage and displays the text
  2. Creates a connection, session, and publisher for the reply message
  3. Publishes the message to the reply topic
  4. Closes the connection

On both servers, it will consume messages from the topic jms/Topic.

Creating and Packaging the Applications

This example uses the connection factory named jms/TopicConnectionFactory and the topic named jms/Topic, both of which you created in Writing Simple JMS Client Applications. It uses an additional connection factory, jms/RemoteTopicConnectionFactory, that communicates with the remote system.

Creating and packaging this application involve several steps:

Starting the J2EE Application Server and Compiling the Source Files

You need to start the J2EE Application Server on both the local and remote systems. You can perform all other tasks on the local system.

  1. Start the J2EE Application Server on the local system, if it is not already running.
  2. Start the J2EE Application Server on the remote system, if it is not already running.
  3. On the local system, go to the directory multiclient. Compile the source files:
  4. asant build

  5. Also on the local system, go to the directory replybean. Compile the source files:
  6. asant build

Creating the Connection Factory

To create a connection factory that will communicate with the remote server, perform the following steps:

  1. Use the following command, replacing sys-name with the name of your remote system:
  2. asadmin create-jms-resource --resourcetype
    javax.jms.TopicConnectionFactory --property
    imqAddressList=sys-name jms/RemoteTopicConnectionFactory

  3. Reconfigure the server:
  4. asant reconfig_common

  5. To verify that the factory was created, use the following command:
  6. asadmin list-jms-resources --user admin

For this example, the remote server must have the TopicConnectionFactory and topic you created in Writing Simple JMS Client Applications. If you have not run any of the tutorial examples on the remote system, run the following asant targets on it:

asant create-tcf
asant create-topic
asant reconfig_common 

Starting the J2EE Deploytool and Creating the Applications

You can perform all the packaging and deployment tasks on the local system.

Do the following to create the application for the client:

  1. Start the deploytool.
  2. Choose FileRight ArrowNewRight ArrowApplication EAR.
  3. Click Browse next to the Application File Name field and use the file chooser to locate the directory multiclient.
  4. In the File Name field, type MultiClientApp.
  5. Click New Application.
  6. Click OK.

Do the following to create the application for the message-driven bean:

  1. Choose FileRight ArrowNewRight ArrowApplication EAR.
  2. Click Browse next to the Application File Name field and use the file chooser to locate the directory replybean.
  3. In the File Name field, type ReplyBeanApp.
  4. Click New Application.
  5. Click OK.

Packaging the Application Client

Perform the following steps to create and package the application client:

  1. Select MultiClientApp and choose FileRight ArrowNewRight ArrowApplication Client JAR to start the Application Client Wizard.
  2. In the JAR File Contents screen:
    1. Verify that Create New AppClient Module in Application is selected and that the application is MultiClientApp.
    2. In the AppClient Display Name field, type MultiAppServerClient.
    3. Click the Edit button next to the Contents text area.
    4. In the dialog box, locate the multiclient/build/ directory. Select MultiAppServerClient.class and MultiAppServerClient$ReplyListener.class from the Available Files tree area and click Add, then OK.
  3. In the General screen, select MultiAppServerClient in the Main Class combo box.
  4. In the Message Destination References screen, click Add. In the dialog box:
    1. Type jms/TopicName in the Coded Name field.
    2. Choose javax.jms.Topic from the Destination Type menu.
    3. Choose Produces from the Usage menu.
    4. Type PhysicalTopic in the Destination Name field.
  5. In the Resource References screen:
    1. Click Add.
    2. Type jms/TopicConnectionFactory1 in the Coded Name field.
    3. Choose javax.jms.ConnectionFactory from the Type menu.
    4. Type jms/TopicConnectionFactory in the JNDI name field and j2ee in both the User Name and Password fields.
    5. Click Add.
    6. Type jms/TopicConnectionFactory2 in the Coded Name field.
    7. Choose javax.jms.ConnectionFactory from the Type menu.
    8. Type jms/RemoteTopicConnectionFactory in the JNDI name field and j2ee in both the User Name and Password fields.
  6. Click Finish.
  7. Click the Message Destinations tab. In the inspector pane:
    1. Click Add.
    2. Type PhysicalTopic in the Destination Name field. When you press Enter, this name appears in the Display Name field, and MultiAppServerClient appears in the Producers area.
    3. Type jms/Topic in the JNDI Name field.

Packaging the Message-Driven Bean

Perform the following steps to package the message-driven bean:

  1. Select ReplyBeanApp and choose FileRight ArrowNewRight ArrowEnterprise JavaBean JAR to start the Enterprise Bean Wizard, then click Next.
  2. In the EJB JAR screen:
    1. Select Create New JAR Module in Application and verify that the application is ReplyBeanApp.
    2. In the JAR Display Name field, type MDBJAR.
    3. Click the Edit button next to the Contents text area.
    4. In the dialog box, locate the replybean/build/ directory. Select ReplyMsgBean.class from the Available Files tree area and click Add, then OK.
  3. In the General screen:
    1. Choose the Message-Driven radio button.
    2. From the Enterprise Bean Class menu, choose ReplyMsgBean.
    3. In the Enterprise Bean Display Name field, accept the default name, ReplyMsgBean.
  4. In the Configuration Options screen, check the Resource References and Transaction Management boxes.
  5. In the Message-Driven Bean Settings screen:
    1. Click Add in the Activation Configuration Properties area.
    2. Type destination in the Property Name field and PhysicalTopic in the Value field.
    3. Click Add again.
    4. Type destinationType in the Property Name field and javax.jms.Topic in the Value field.
    5. Choose javax.jms.Topic from the Destination Type menu.
    6. Type PhysicalTopic in the Destination Name field.
    7. Click Deployment Settings. In the dialog box, type jmsra in the Resource Adapter field and click OK.
  6. In the Transaction Management screen, choose Container-Managed.
  7. In the Resource References screen:
    1. Click Add.
    2. Type jms/TopicConnectionFactory in the Coded Name field.
    3. Choose javax.jms.ConnectionFactory from the Type menu.
    4. Type jms/TopicConnectionFactory in the JNDI name field and j2ee in both the User Name and Password fields.
  8. Click Finish.
  9. Click the Message Destinations tab. In the inspector pane:
    1. Click Add.
    2. Type PhysicalTopic in the Destination Name field. When you press Enter, this name appears in the Display Name field, and ReplyMsgBean appears in the Consumers area.
    3. Type jms/Topic in the JNDI Name field.

Verifying and Updating the JNDI Names

You need to update the JNDI name for the RemoteMDBApp application and verify it for the MultiClientApp application.

  1. Select the ReplyBeanApp application and click the JNDI Names tab.
  2. Type jms/Topic in the JNDI Name field for ReplyMsgBean.

The Application Pane for ReplyBeanApp should appear as shown in Table 21-10.

Table 21-10 Application Pane for ReplyBeanApp
Component Type
Component
JNDI Name
EJB
ReplyMsgBean
jms/Topic

The References Pane for ReplyBeanApp should appear as shown in Table 21-11.

Table 21-11 References Pane for ReplyBeanApp
Ref. Type
Referenced By
Reference Name
JNDI Name
Resource
ReplyMsgBean
jms/TopicConnectionFactory
jms/TopicConnectionFactory

Select the MultiClientApp application and click the JNDI Names tab.

The JNDI names for the application should appear as shown in Table 21-12. Only the References Pane has any content.

Table 21-12 References Pane for MultiClientApp
Ref. Type
Referenced By
Reference Name
JNDI Name
Resource
MultiAppServerClient
jms/TopicConnectionFactory1
jms/TopicConnectionFactory
Resource
MultiAppServerClient
jms/TopicConnectionFactory2
jms/RemoteTopicConnectionFactory

Deploying the Applications

To deploy the MultiClientApp application and the ReplyBeanApp application on the local server, perform the following steps for each application:

  1. Choose FileRight ArrowSave to save the application.
  2. Choose ToolsRight ArrowDeploy.
  3. In the dialog box, choose the URI for localhost from the menu:
  4. deployer:Sun:S1AS::localhost:4848

  5. Type your administrative user name and password (if they are not already filled in) and click OK.
  6. In the Distribute Module dialog box, click Close when the process completes.

Before you can deploy the ReplyBeanAppApp application, you must add the remote server, if you did not do so before. Perform the following steps:

  1. Choose FileRight ArrowAdd Server.
  2. Type the name of the server in the Server Name field and click OK.
  3. The server appears in the tree under Servers. Select it.
  4. In the dialog box that appears, type the administrative user name and password for the server in the Connection Settings area and click OK.

To deploy the ReplyBeanApp application on the remote server, perform the following steps:

  1. Choose FileRight ArrowSave to save the application.
  2. Choose ToolsRight ArrowDeploy.
  3. In the dialog box, choose the URI with the name of the remote system from the menu.
  4. Type your administrative user name and password (if they are not already filled in) and click OK.
  5. In the Distribute Module dialog box, click Close when the process completes.

Saving the Client JAR and Running the Application Client

Perform the following steps:

  1. In the deploytool, select localhost.
  2. Select MultiClientApp from the list of deployed objects, then click Client Jar.
  3. Choose Browse to navigate to the directory from which you will run the client. Specify the sendremote/multiclient/ directory. When you reach the directory, click Select, then click OK.
  4. Click OK in the information dialog. You will find a file named MultiClientAppClient.jar in the specified directory.
  5. To run the client, use the following command:
  6. appclient -client MultiClientAppClient.jar

On the local system, the output of the appclient command looks something like this:

Binding name:`java:comp/env/jms/TopicName`
Sent message: text: id=1 to local app server
Sent message: text: id=2 to remote app server
ReplyListener: Received message: id=1, text=ReplyMsgBean 
processed message: text: id=1 to local app server
Sent message: text: id=3 to local app server
ReplyListener: Received message: id=3, text=ReplyMsgBean 
processed message: text: id=3 to local app server
ReplyListener: Received message: id=2, text=ReplyMsgBean 
processed message: text: id=2 to remote app server
Sent message: text: id=4 to remote app server
ReplyListener: Received message: id=4, text=ReplyMsgBean 
processed message: text: id=4 to remote app server
Sent message: text: id=5 to local app server
ReplyListener: Received message: id=5, text=ReplyMsgBean 
processed message: text: id=5 to local app server
Sent message: text: id=6 to remote app server
ReplyListener: Received message: id=6, text=ReplyMsgBean 
processed message: text: id=6 to remote app server
Sent message: text: id=7 to local app server
ReplyListener: Received message: id=7, text=ReplyMsgBean 
processed message: text: id=7 to local app server
Sent message: text: id=8 to remote app server
ReplyListener: Received message: id=8, text=ReplyMsgBean 
processed message: text: id=8 to remote app server
Sent message: text: id=9 to local app server
ReplyListener: Received message: id=9, text=ReplyMsgBean 
processed message: text: id=9 to local app server
Sent message: text: id=10 to remote app server
ReplyListener: Received message: id=10, text=ReplyMsgBean 
processed message: text: id=10 to remote app server
Waiting for 0 message(s) from local app server
Waiting for 0 message(s) from remote app server
Finished
Closing connection 1
Closing connection 2 

On the local system, where the message-driven bean receives the odd-numbered messages, the output in the server log looks like this:

In ReplyMsgBean.ReplyMsgBean()
In ReplyMsgBean.setMessageDrivenContext()
In ReplyMsgBean.ejbCreate()
  ReplyMsgBean: Received message: text: id=1 to local app server
  ReplyMsgBean: Received message: text: id=3 to local app server
  ReplyMsgBean: Received message: text: id=5 to local app server
  ReplyMsgBean: Received message: text: id=7 to local app server
  ReplyMsgBean: Received message: text: id=9 to local app server 

On the remote system, where the bean receives the even-numbered messages, the output in the server log looks like this:

In ReplyMsgBean.ReplyMsgBean()
In ReplyMsgBean.setMessageDrivenContext()
In ReplyMsgBean.ejbCreate()
  ReplyMsgBean: Received message: text: id=2 to remote app 
server
  ReplyMsgBean: Received message: text: id=4 to remote app 
server
  ReplyMsgBean: Received message: text: id=6 to remote app 
server
  ReplyMsgBean: Received message: text: id=8 to remote app 
server
  ReplyMsgBean: Received message: text: id=10 to remote app 
server 

Undeploying and Removing the Applications

To undeploy and remove the applications, perform the following steps:

  1. Select localhost.
  2. Select MultiClientApp in the server pane and click Undeploy.
  3. Select the application in the left-hand pane and choose FileRight ArrowClose.
  4. Repeat steps 1-3 for ReplyBeanApp on the local system.
  5. Repeat steps 1-3 for ReplyBeanApp on the remote system.
  6. Remove the build directory:
  7. asant clean

  8. If you wish, stop the server.

If you wish, you can manually delete the EAR and client JAR files.

Divider
Download
FAQ
History
HomeHomeNext API
Search
Feedback
Divider

All of the material in The J2EE 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.